package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.*;
import com.epfresh.supplier.entity.*;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BookGoodsYearActivity extends BaseActivity {


    private TextView tv_time_now,tv_money_now,tv_title,tv_package,tv_number;
    ImageView iv_product;
    ListView lv_deal_detail;
    BookGoodsYearAdapter bookYearGoodsAdapter;

    ArrayList<BookGoodsYear.MonthIncomeListBean> list = new ArrayList<>();

    String timeTitle,money,timeValue,productId,type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_goods_year);
        ivBack.setVisibility(View.VISIBLE);
        View headView;
        headView = getLayoutInflater().inflate(R.layout.include_book_head_goods_year, null);


        money = getIntent().getStringExtra("money");
        timeValue = getIntent().getStringExtra("timeValue");
        timeTitle = getIntent().getStringExtra("timeTitle");
        productId = getIntent().getStringExtra("productId");
        type = getIntent().getStringExtra("type");

        toolbarTitle.setText(timeTitle+"商品销售详情");
        iv_product = (ImageView) headView.findViewById(R.id.iv_product);
        iv_product = (ImageView) headView.findViewById(R.id.iv_product);

        tv_time_now = (TextView) headView.findViewById(R.id.tv_time_now);
        tv_money_now = (TextView) headView.findViewById(R.id.tv_money_now);
        tv_time_now.setText(timeTitle);
        tv_money_now.setText(money);

        tv_title = (TextView) headView.findViewById(R.id.tv_title);
        tv_package = (TextView) headView.findViewById(R.id.tv_package);
        tv_number = (TextView)headView.findViewById(R.id.tv_number);

        lv_deal_detail = (ListView)findViewById(R.id.lv_deal_detail);
        lv_deal_detail.addHeaderView(headView);
        bookYearGoodsAdapter = new BookGoodsYearAdapter(this,list);
        bookYearGoodsAdapter.setOnItemClickListener(this);
        lv_deal_detail.setAdapter(bookYearGoodsAdapter);

        mHttpLoad();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ll_item:
                Object o = view.getTag(R.id.item_key_position);
                if (o != null && o instanceof Integer) {
                    int position = (int) o;
                    Intent detailIntent = new Intent(this,
                            BookGoodsMonthActivity.class);
                    detailIntent.putExtra("timeTitle", list.get(position).getName());
                    detailIntent.putExtra("type", list.get(position).getType() + "");
                    detailIntent.putExtra("timeValue", list.get(position).getQueryDate());
                    detailIntent.putExtra("money", ""+list.get(position).getPrice()+"元");
                    detailIntent.putExtra("productId", list.get(position).getSpecId());
                    startActivity(detailIntent);
                }
                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("timeValue",timeValue);
        outState.putString("productId",productId);
        outState.putString("money",money);
        outState.putString("timeTitle",timeTitle);
        outState.putString("type",type);
        super.onSaveInstanceState(outState);
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForMonthOfYear);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("queryDate", timeValue);
        map.put("accountId",getUser().getAccountId());
        map.put("specId", productId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForMonthOfYear, onRequestListener);
    }

    private OnRequestListener<BookGoodsYear> onRequestListener = new OnRequestListener<BookGoodsYear>() {
        @Override
        public BookGoodsYear jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BookGoodsYear.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BookGoodsYear> response, Object tag) {

            hideProgressDialog();
            BookGoodsYear responseElement = response.getResponseElement();

            tv_title.setText(responseElement.getProductTitle());
            tv_package.setText(responseElement.getPack());
            tv_number.setText(responseElement.getProductCountTotleTitle());
            ImageLoader.getInstance().displayImage(responseElement.getProductImg(), iv_product);

            list.clear();
            list.addAll(responseElement.getMonthIncomeList());
            bookYearGoodsAdapter.notifyDataSetChanged();
        }
    };

}
